package pers.dawnyang.modular.system.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import pers.dawnyang.common.annotation.OperLog;
import pers.dawnyang.common.domain.Result;
import pers.dawnyang.common.domain.entity.SysDept;
import pers.dawnyang.common.enums.OperType;
import pers.dawnyang.common.util.ResultUtil;
import pers.dawnyang.modular.system.domain.request.dept.DeptReqC;
import pers.dawnyang.modular.system.domain.request.dept.DeptReqR;
import pers.dawnyang.modular.system.domain.request.dept.DeptReqU;
import pers.dawnyang.modular.system.domain.response.dept.DeptDtree;
import pers.dawnyang.modular.system.domain.response.dept.DeptRes;
import pers.dawnyang.modular.system.service.DeptService;

/**
 * 部门管理
 * 
 * @author dawn yang
 * 
 */

@RequestMapping("/sys/dept")
@Controller
@Validated
public class DeptController {

  @Autowired
  private DeptService deptService;

  @RequiresPermissions("sys:dept:index")
  @GetMapping("/index")
  public String index() {
    return "/sys/dept/index";
  }

  @RequiresPermissions("sys:dept:add")
  @GetMapping("/addView")
  public String addView() {
    return "/sys/dept/addView";
  }

  @RequiresPermissions("sys:dept:edit")
  @GetMapping("/editView")
  public String editView(@RequestParam("id") Long id, ModelMap model) {
    SysDept sysDept = deptService.getById(id);
    model.put("model", JSON.toJSONString(sysDept));
    return "/sys/dept/editView";
  }

  /**
   * Dtree下拉树结果
   * 
   * @return
   */
  @RequiresPermissions({"sys:dept:add", "sys:dept:edit"})
  @GetMapping("/getDtreeList")
  @ResponseBody
  public Result<List<DeptDtree>> getDtreeList() {
    List<DeptDtree> resList = deptService.getDtreeList();
    return ResultUtil.success(resList);
  }

  // 递归查询所有树节点
  /*
   * @GetMapping("/getTreeList")
   * 
   * @ResponseBody public Result<List<DeptRes>> getTreeList() { List<DeptRes> resList =
   * deptService.getTreeList(Long.valueOf(1)); return ResultUtil.success(resList); }
   */

  /**
   * 一次性返回所有list，前端处理树结构
   * 
   * @return
   */
  @RequiresPermissions("sys:dept:list")
  @PostMapping("/list")
  @ResponseBody
  public Result<List<DeptRes>> getList(DeptReqR req) {
    List<DeptRes> resList = deptService.getList(req);
    return ResultUtil.success(resList);
  }

  /**
   * 分页树查询
   * 
   * @param page
   * @param req
   * @return
   */
  @RequiresPermissions("sys:dept:list")
  @PostMapping("/getTreePage")
  @ResponseBody
  public Result<Page<DeptRes>> getTreePage(Page<DeptRes> page, DeptReqR req) {
    Page<DeptRes> pageRes = deptService.getTreePage(page, req);
    return ResultUtil.success(pageRes);
  }

  @OperLog(operModul = "机构管理", operType = OperType.UPDATE)
  @RequiresPermissions("sys:dept:edit")
  @PostMapping("/edit")
  @ResponseBody
  public Result<String> edit(@Validated DeptReqU req) {
    SysDept sysDept = new SysDept();
    BeanUtils.copyProperties(req, sysDept);

    if (req.getParentId() != 0) {
      SysDept sysDeptP = deptService.getById(req.getParentId());
      sysDept.setPids(sysDeptP.getPids() + "," + req.getParentId());
    } else {
      sysDept.setPids("0");
    }

    deptService.updateById(sysDept);
    return ResultUtil.success();
  }

  @OperLog(operModul = "机构管理", operType = OperType.INSERT)
  @RequiresPermissions("sys:dept:add")
  @PostMapping("/add")
  @ResponseBody
  public Result<String> add(@Validated DeptReqC req) {
    SysDept sysDept = new SysDept();
    BeanUtils.copyProperties(req, sysDept);
    if (req.getParentId() != 0) {
      SysDept sysDeptP = deptService.getById(req.getParentId());
      sysDept.setPids(sysDeptP.getPids() + "," + req.getParentId());
    } else {
      sysDept.setPids("0");
    }
    deptService.save(sysDept);
    return ResultUtil.success();
  }

  @OperLog(operModul = "机构管理", operType = OperType.DELETE)
  @RequiresPermissions("sys:dept:del")
  @PostMapping("/del")
  @ResponseBody
  public Result<String> del(@RequestParam("id") Long id) {
    SysDept sysDept = new SysDept();
    sysDept.setDeptId(id);
    sysDept.setStatus(0);
    deptService.updateById(sysDept);
    return ResultUtil.success();
  }

}
